﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/6/30 17:36:44
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    class PDPLabel:AbstractLabelProvider
    {
        public PDPLabel(string label)
            : base(label)
        {

        }
       
        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);

                if (paras.Length < 1)
                {
                    return this.LabelString;
                }

                string prop = paras[0];
                switch (prop.ToLower())
                {
                    case "url":
                        if (this.Context.State is Product)
                        {
                            return this.GroupLogicFactory.ProductLogic.GetURL(this.Context.State as Product);
                        }
                        else
                        {
                            return this.LabelString;
                        }
                    case "title":
                        string title = typeof(Product).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        if (paras.Length > 1)
                        {
                            int l = 0;
                            if (int.TryParse(paras[1], out l))
                            {
                                return title.ToShortFormat(l);
                            }
                            else
                            {
                                return title;
                            }
                        }
                        else
                        {
                            return title;
                        }
                    case "productico":
                        string ico = typeof(Product).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        return System.Web.VirtualPathUtility.ToAbsolute(ico);
                    case "productimage":
                        string img = typeof(Product).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        return System.Web.VirtualPathUtility.ToAbsolute(img);
                    default:
                        // 如果上下文信息是文章则处理
                        if (this.Context.State is Product)
                        {
                            return typeof(Product).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        }
                        else
                        {
                            return this.LabelString;
                        }
                }

            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
